﻿using System;
using System.Windows.Input;

namespace XamlInspector.Foundation
{
    public class Command : ICommand
    {
        private readonly Action<object> _execute;
        private readonly Predicate<object> _canExecute;

        public Command(Action<object> execute, Predicate<object> canExecute = null)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public Command(Action execute, Predicate<object> canExecute = null)
        {
            _execute = (o => execute());
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecute != null)
            {
                return _canExecute(parameter);
            }
            
            return true;
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        public void FireCanExecuteChanged()
        {
            CanExecuteChanged.Fire(this);
        }

        public event EventHandler CanExecuteChanged;
    }
}
